package com.lemon.cloud.admin.web;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.admin.dto.LogDTO;
import com.lemon.cloud.admin.entity.SysLog;
import com.lemon.cloud.admin.service.SysLogService;
import com.lemon.cloud.comm.constants.enums.LogBusType;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/log")
@Tag(name = "日志接口")
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;

    @PostMapping
    @Inner
    public ResultMsg<Boolean> saveLog(@Valid @RequestBody SysLog sysLog) {
        return ResultMsg.resultSuccess(sysLogService.save(sysLog));
    }

    /**
     * 分页查询
     * @param page
     * @param logDTO
     * @return
     */
    @Operation(
            summary = "分页查询",description = "分页日志",
            responses = {
                    @ApiResponse(responseCode = "200",description = "successful operation",content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,schema = @Schema(implementation = IPage.class))},useReturnTypeSchema = true)
            }
    )
    @GetMapping("/page")
    public ResultMsg<IPage<SysLog>> page(@ParameterObject Page<SysLog> page, @ParameterObject LogDTO logDTO){
        LambdaQueryWrapper<SysLog> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(logDTO.getType())) {
            wrapper.eq(SysLog::getType, logDTO.getType());
        }
        if (logDTO.getIsNormal()!=null){
            if (logDTO.getIsNormal()){
                wrapper.isNull(SysLog::getException);
            }else {
                wrapper.isNotNull(SysLog::getException);
            }
        }
        if (ArrayUtil.isNotEmpty(logDTO.getCreateTime())) {
            wrapper.ge(SysLog::getCreateTime, logDTO.getCreateTime()[0]).le(SysLog::getCreateTime,
                    logDTO.getCreateTime()[1]);
        }
        return ResultMsg.resultSuccess(sysLogService.page(page, wrapper));
    }

    @Operation(
            summary = "删除日志",description = "删除日志",
            parameters = {@Parameter(name = "id",description = "id",in = ParameterIn.PATH,required = true)},
            responses = {
                    @ApiResponse(responseCode = "200",description = "successful operation"),
                    @ApiResponse(responseCode = "400",description = "参数错误")
            }
    )
    @DeleteMapping("{id}")
    @com.lemon.cloud.log.annotation.SysLog(value = "删除日志",businessType = LogBusType.DELETE)
    @PreAuthorize("hasAnyAuthority('sys_log_del')")
    public ResultMsg<Boolean> delLog(@PathVariable("id") Long id){
        return ResultMsg.resultSuccess(sysLogService.removeById(id));

    }
}
